﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace ZXGZTJ_存储过程生成程序
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           string data2= ExSystem.ExIO.ExFile.ReadToEnd(Application.StartupPath + "\\config.xml");
           string model_file = ExSystem.ExIO.ExFile.ReadToEnd(Application.StartupPath + "\\ZXGZTJ模板.txt",Encoding.UTF8);
           string model_file_fc = ExSystem.ExIO.ExFile.ReadToEnd(Application.StartupPath + "\\ZXGZTJ_FC模板.txt", Encoding.UTF8);
           XmlDocument doc = new XmlDocument();
           doc.LoadXml(data2);
           XmlNodeList list = doc.SelectNodes("root/page");
           foreach (XmlNode node in list)
           {
               string file_txt = model_file;
               string tablename = node.Attributes["table_name"].Value;
               string temp_Table_name = "SESSION.#Temp_" + tablename;
               string pro_name = node.Attributes["por_name"].Value;
               string ksrq_name = node.Attributes["ksrq_name"].Value;
               string data= node.SelectSingleNode("rules").InnerText;
              
               int columnCount = int.Parse(node.Attributes["columns"].Value);
               file_txt = file_txt.Replace("<%存储过程名%>", pro_name);
               file_txt = file_txt.Replace("<%DECLARE V_COL01 DOUBLE DEFAULT 0;%>", creatA1(columnCount));
               file_txt = file_txt.Replace("<%DECLARE HJ_V_COL01 DOUBLE DEFAULT 0;%>", creatA2(columnCount));
               file_txt = file_txt.Replace("<%COL01	DOUBLE DEFAULT 0,%>", creatA3(columnCount));

               file_txt = file_txt.Replace("<%计算法规_法院%>", creatA4(data));
               file_txt = file_txt.Replace("<%,V_COL01%>", creatA9(columnCount));
               file_txt = file_txt.Replace("<%计算法规_庭室%>", creatA5(data));
               file_txt = file_txt.Replace("<%计算法规_人员(未指定承办法官)%>", creatA6(data));
               file_txt = file_txt.Replace("<%计算法规_人员%>", creatA7(data));
               file_txt = file_txt.Replace("<%,HJ_V_COL01%>", creatA11(columnCount));
               file_txt = file_txt.Replace("<%,COL01%>", creatA10(columnCount));
               
               file_txt = file_txt.Replace("<%SET HJ_V_COL01=HJ_V_COL01+V_COL01;%>", creatA8(columnCount));
               file_txt = file_txt.Replace("<%数据临时表名%>", temp_Table_name);
               file_txt = file_txt.Replace("<%表名%>", tablename);
               file_txt = file_txt.Replace("<%开始日期名称%>", ksrq_name);
               
               ExSystem.ExIO.ExFile.Save(file_txt, string.Format("c:\\新{0}.txt", pro_name), Encoding.UTF8);

               string file_txt_fc = model_file_fc;
               string pro_name_fc = pro_name + "_FC";
               string dingyi = node.SelectSingleNode("fc_fields").InnerText;
               file_txt_fc = file_txt_fc.Replace("<%反查存储过程名%>", pro_name_fc);
               file_txt_fc = file_txt_fc.Replace("<%反查临时表名%>", temp_Table_name);
               file_txt_fc = file_txt_fc.Replace("<%反查临时表字段定义%>", dingyi);
               file_txt_fc = file_txt_fc.Replace("<%,反查字段%>", fc1(dingyi));
               file_txt_fc = file_txt_fc.Replace("<%表名%>", tablename);
               file_txt_fc = file_txt_fc.Replace("<%开始日期名称%>", ksrq_name);
               file_txt_fc = file_txt_fc.Replace("<%反查计算规则SQL语句组织%>", fc2(data));

               ExSystem.ExIO.ExFile.Save(file_txt_fc, string.Format("c:\\新{0}.txt", pro_name_fc), Encoding.UTF8);

           }
        }

        //<%,反查字段%>
        private string fc1(string dingyi)
        {
           string[] a= ExSystem.ExString.SplitTwice(dingyi, ",", " ", 0, StringSplitOptions.None);
            string b= ExSystem.ExString.ToString(a, "", ",", "").Replace(System.Environment.NewLine,"");
            return b;
        }

        //<%反查计算规则SQL语句组织%>
        private string fc2(string data)
        {
            StringBuilder builder = new StringBuilder();
            string[] a = ExSystem.ExString.SplitTwice(data, ";", " WHERE ", 1, StringSplitOptions.None);
            for(int i=0;i<a.Length;i++)
            {
                if (string.IsNullOrEmpty(a[i].Trim())) continue;
                a[i] = a[i].Replace("'", "''");
                builder.AppendLine(string.Format("WHEN {0} THEN", i + 1));
                builder.AppendLine(string.Format("set sql_Str=sql_Str||' AND {0})';", a[i]).Replace("TJKSRQ", "'''||CHAR(TJKSRQ)||'''").Replace("TJJSRQ","'''||CHAR(TJJSRQ)||'''"));
            }
            return builder.ToString();
        }


        private string creatA1(int count)
        {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                b.AppendLine(string.Format("DECLARE V_COL{0} DOUBLE DEFAULT 0;",(i+1).ToString("00")));
            }
            return b.ToString();
        }
        private string creatA2(int count)
        {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                b.AppendLine(string.Format("DECLARE HJ_V_COL{0} DOUBLE DEFAULT 0;", (i + 1).ToString("00")));
            }
            return b.ToString();
        }
        private string creatA3(int count)
        {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                b.AppendLine(string.Format("COL{0} DOUBLE DEFAULT 0,", (i + 1).ToString("00")));
            }
            return ExSystem.ExString.RemoveLastString(b.ToString(),",");
        }
        private string creatA4(string data)
        {

            return data.Replace(";", " AND FYDM = V_FYDM;");
        }
        private string creatA5(string data)
        {

            return data.Replace(";", " AND (CBSPTBS=V_TSDM or CBTS=V_TSMC);");
        }
        private string creatA6(string data)
        {

            return data.Replace(";", " AND (CBR=V_RYMC or CBR IS NULL);");
        }
        private string creatA7(string data)
        {

            return data.Replace(";", " AND CBR=V_RYMC;");
        }
        private string creatA8(int count)
        {

            StringBuilder b = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                b.AppendLine(string.Format("SET HJ_V_COL{0}=HJ_V_COL{0}+V_COL{0};", (i + 1).ToString("00")));
            }
            return b.ToString();
        }
        private string creatA9(int count)
        {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                b.Append(string.Format(",V_COL{0}", (i + 1).ToString("00")));
            }
            return b.ToString();
        }
        private string creatA10(int count)
        {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                b.Append(string.Format(",COL{0}", (i + 1).ToString("00")));
            }
            return b.ToString();
        }
        private string creatA11(int count)
        {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                b.Append(string.Format(",HJ_V_COL{0}", (i + 1).ToString("00")));
            }
            return b.ToString();
        }
        
    }
}
